package cn.zcy.springboot_web.controller;

import cn.zcy.springboot_web.entity.User;
import cn.zcy.springboot_web.result.Result;
import cn.zcy.springboot_web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户的Controller")
class UserController {
    @Autowired
    IUserService userService;

    @Autowired
    MessageSource messageSource;

    @GetMapping("/{id}")
    @ApiOperation("根据用户id获取用户信息的接口")
    @ApiImplicitParam(name = "id",value = "用户id")
    public Result select(@PathVariable("id") Integer id) {
    //http://localhost:8081/user/1?locale=en_US  LocaleChangeInterceptor  默认的 locale
        //LocaleContextHolder就是local的一个持有器，spingmvc底层会自动将LocalResovler中的语言设置进去
      String message=  messageSource.getMessage("user.query.success", null,LocaleContextHolder.getLocale());
        User user = userService.selectUserById(id);
        return new Result(200, message, user);
    }

    @GetMapping("/list")
    public Result selectUserList(User user) {
        List<User> userList = userService.selectUserList(user);
        return new Result(200, "查询成功", userList);
    }

    @PostMapping
    public Result addUser(User user) {
        int i = userService.addUser(user);
        return new Result(200, "新增成功", i);
    }

    @PutMapping("/{id}")
    public Result updateUser(User user) {
        int i = userService.updateUser(user);
        return new Result(200, "修改成功", i);
    }

    @DeleteMapping("/{id}")
    public Result deleteUserById(@PathVariable(value = "id") Integer id) {
        int i = userService.deleteUserById(id);
        return new Result(200, "修改成功", i);
    }
}
